//
//  MorePictureView.m
//  SimpleSrore
//
//  Created by gaoguangxiao on 2017/3/25.
//  Copyright © 2017年 高广校. All rights reserved.
//

#import "MorePictureView.h"


#import "EntityAd.h"
@implementation MorePictureView

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */
-(id)initMorePictureItemWithFram:(CGRect)frame andData:(NSArray *)paramData;
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        [self addSubview:self.bgimageView];
        [self.bgimageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@10);
            make.right.equalTo(@-10);
            make.top.bottom.equalTo(self);
        }];
        
        [self.bgimageView addSubview:self.scrollView];
        [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bgimageView).offset([self backLeftMaginHeight]);
            make.right.equalTo(self.bgimageView).offset(-60);
            make.height.equalTo(@30);
            make.top.equalTo(self.bgimageView).offset([self backtopMaginHeight]);
        }];
        
        CGSize sizeSupper = self.frame.size;
        //加顶部公告20
        CGFloat startY    = 25 + 20;
        //底部额外增加边距25，
        CGFloat bottomMagin = 25 + 25;
        CGFloat sepHeight = (sizeSupper.height - startY - bottomMagin)/2.0;//分割高度
        
        for (NSInteger i = 0 ; i < paramData.count; i++) {
            MorePictureTwoView *topView = [[MorePictureTwoView alloc] initItemWithFram:CGRectMake(20, startY + sepHeight * i, sizeSupper.width - 40, sepHeight) andData:paramData[i]andRow:i*10];
            topView.delegate = self;
            [self addSubview:topView];
        }
        
        
        
    }
    return self;
}
-(CGFloat)backLeftMaginHeight{
    if (SCREEN_WIDTH == 320){
        if (SCREEN_HEIGHT == 480) {
            return 70;
        }else{
            return 80;
        }
    }else{
        if (SCREEN_HEIGHT == 667) {
            return 95;
        }else{
            return 100;
            
        }
    }
    
}

-(CGFloat)backtopMaginHeight{
    if (SCREEN_WIDTH == 320){
        if (SCREEN_HEIGHT == 480) {
            return 15;
        }else{
            return 5;
        }
    }else{
        if (SCREEN_HEIGHT == 667) {
            return 20;
        }else{
            return 20;

        }
    }
    
}

-(void)Init{
    [super Init];
}

-(void)setDataNotice:(NSArray *)dataNotice{
    
    _dataNotice = [dataNotice  copy];
    NSArray *dataList = _dataNotice;
//
    for (UIView *v in _scrollView.subviews) {
        [v removeFromSuperview];
    }
    
    NSMutableString *mutableString = [NSMutableString new];
    [dataList enumerateObjectsUsingBlock:^(EntityAd *  _Nonnull obj, NSUInteger i, BOOL * _Nonnull stop) {
        if (i==0) {//最新的咨询
             [mutableString appendString:obj.zhaiyao];
        }
        
//        UIView *backView  =[[UIView alloc]initWithFrame:CGRectMake(0, _scrollView.frame.size.height * i, _scrollView.frame.size.width, 30)];
////        backView.userInteractionEnabled = YES;
//        [_scrollView addSubview:backView];
//        
//        UILabel *labelShow = [[UILabel alloc]initWithFrame:CGRectMake(0, 2, _scrollView.frame.size.width, _scrollView.frame.size.height)];
//        labelShow.textAlignment= NSTextAlignmentLeft;
//        labelShow.userInteractionEnabled  = YES;
//        NSMutableAttributedString *tip = [[NSMutableAttributedString alloc] init];
//        [tip appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", obj.title] attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:11.0f]}]];
//
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touchedAction:)];
//        [labelShow addGestureRecognizer:tap];
//        labelShow.attributedText = tip;
//        labelShow.tag = i;
//        [backView addSubview:labelShow];
//        
    }];

    CGSize size       = [mutableString boundingRectWithSize:CGSizeMake(0, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0]} context:nil].size;
    //        //计算动画时间 保证一个屏幕需要移动动画1秒
    CGFloat timerInterVal = size.width/SCREEN_WIDTH * 4;
    [self createMoveLeble:mutableString andIndex:0 andDuation:timerInterVal];
//
//    CGSize size = CGSizeMake(self.scrollView.bounds.size.height, dataNotice.count*self.scrollView.bounds.size.height);
//    _scrollView.contentSize = size;
}
-(void)createMoveLeble:(NSString *)title andIndex:(NSInteger)index andDuation:(CGFloat)duration{
    
    //需要移除
    [self.scrollView addSubview:self.demoLabelOne];
    //创建滚动的文字，测试
    
    _demoLabelOne.text = title;
    //
    [_demoLabelOne resetLabel];
    _demoLabelOne.tag  = index;
    _demoLabelOne.scrollDuration = duration;
    
    
    
}
-(MarqueeLabel *)demoLabelOne{
    if (!_demoLabelOne) {
        _demoLabelOne = [[MarqueeLabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
    }
    _demoLabelOne.textColor = [UIColor whiteColor];
    _demoLabelOne.font = [UIFont systemFontOfSize:15.0];
    _demoLabelOne.leadingBuffer = 30.0f;
    _demoLabelOne.trailingBuffer = 50.0f;
    _demoLabelOne.marqueeType = MLContinuous;
    
    _demoLabelOne.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touchedAction:)];
    [_demoLabelOne addGestureRecognizer:tap];
    return _demoLabelOne;
    
}

-(UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [UIScrollView new];
        _scrollView.bounds = CGRectMake(0, 0, SCREEN_WIDTH, 30);
//        _scrollView.userInteractionEnabled = YES;
    }
    return _scrollView;
}
-(UIImageView *)bgimageView{
    if (!_bgimageView) {
        _bgimageView = [UIImageView new];
        _bgimageView.image = [UIImage imageNamed:@"HomecenterBg"];
        _bgimageView.contentMode  = UIViewContentModeScaleToFill;
        _bgimageView.userInteractionEnabled = YES;
    }
    return _bgimageView;
}
-(void)didMorePictureTwoViewBtnItem:(NSInteger)tagBtn{
    if ([self.delegate respondsToSelector:@selector(didMorePictureViewBtn:)]) {
        [self.delegate didMorePictureViewBtn:tagBtn];
    }
}
-(void)touchedAction:(UITapGestureRecognizer *)sender{
    if (self.delegate && [_delegate respondsToSelector:@selector(clickedByHomeAtIndex:data:)]) {
        [_delegate clickedByHomeAtIndex:sender.view.tag data:_dataNotice[sender.view.tag]];
    }
}
@end
